<template>
  <!-- 最外层容器 -->
  <el-container class="home-container">
    <!-- 头部 -->
    <el-header>
      <div class="logo">
        <img src="@/assets/boy.svg" />
        <span class="header-text"> 管理系统 </span>
      </div>
      <el-button type="info" @click="quit">退出</el-button></el-header
    >
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isTouchToggleButton ? '64px' : '200px'">
        <!-- menu容器 -->
        <div class="toggle-button" @click="togglebutton">|||</div>
        <el-menu
          background-color="#323744"
          text-color="#fff"
          active-text-color="#409eff"
          :unique-opened="true"
          :collapse="isTouchToggleButton"
          :collapse-transition="false"
          :router="true"
          :default-active="activatePath"
        >
          <!-- 一级菜单 -->
          <el-submenu :index="item.id" v-for="item in menuList" :key="item.id">
            <template slot="title">
              <i class="el-icon-location"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + items.path"
              v-for="items in item.children"
              :key="items.id"
              @click="saveActivePath('/' + items.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ items.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menuList: [],
      isTouchToggleButton: false,
      activatePath: ''
    }
  },
  created() {
    this.InitFornList()
    // this.activatePath = window.sessionStorage.getItem('activatePath')
  },

  methods: {
    quit() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    saveActivePath(activatePath) {
      // window.sessionStorage.setItem('activatePath', activatePath)
      this.activatePath = activatePath
    },
    togglebutton() {
      this.isTouchToggleButton = !this.isTouchToggleButton
    },
    InitFornList() {
      // 正常是跟服务器取数据
      // const {data:res} = await this.$http.get('menu')  别忘记方法前加上anysc
      // if(res.meta.status !== 200) return this.$message.error({message:"获取商品列表出现问题"})
      // this.menuList = res.data
      // 获取菜单数据，为了方便构建页面，自己创建数据
      this.menuList = [
        {
          id: '101',
          authName: '用户管理',
          path: 1,
          children: [
            {
              id: 120,
              authName: '用户列表',
              path: 'users',
              children: []
            }
          ]
        },
        {
          id: '102',
          authName: '权限管理',
          path: 2,
          children: [
            {
              id: '121',
              authName: '角色列表',
              path: 'roles',
              children: []
            },
            {
              id: '131',
              authName: '权限列表',
              path: 'admins',
              children: []
            }
          ]
        },
        {
          id: '103',
          authName: '商品管理',
          path: 3,
          children: [
            {
              id: '123',
              authName: '商品列表',
              path: 'productions',
              children: []
            }
          ]
        },
        {
          id: '104',
          authName: '订单管理',
          path: 4,
          children: [
            {
              id: '124',
              authName: '商品列表',
              path: 'orders',
              children: []
            }
          ]
        },
        {
          id: '105',
          authName: '数据统计',
          path: 5,
          children: [
            {
              id: '125',
              authName: '商品列表',
              path: 'data',
              children: []
            }
          ]
        }
      ]
    }
  }
}
</script>

<style lang="scss" scoped>
.el-header {
  background-color: #373c41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  img {
    height: 50px;
    width: 50px;
    margin-right: 10px;
  }
  span {
    font-size: 30px;
  }
  div {
    display: flex;
    align-items: center;
    color: white;
  }
}

.el-aside {
  background-color: #323744;
  .el-menu {
    border-right: none;
  }
  .toggle-button {
    line-height: 30px;
    font-size: 10px;
    text-align: center;
    letter-spacing: 0.2em;
    color: white;
    cursor: pointer;
  }
}

.el-main {
  background-color: #eaedf2;
}
.home-container {
  height: 100%;
}
</style>
